package pres.moling.common.spring;

import jakarta.servlet.ServletContext;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.ServletContextAware;

public class SpringHelper implements ApplicationContextAware, ServletContextAware {

    private static ApplicationContext springRootApplicationContext;
    private static ServletContext servletContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        springRootApplicationContext = applicationContext;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;

    }

    public static <T> T getSpringBean(String name) {
        if (springRootApplicationContext.containsBean(name)) {
            return (T) springRootApplicationContext.getBean(name);
        } else return null;
    }

    public static <T> T getSpringBean(Class<T> clazz) {
        return springRootApplicationContext.getBean(clazz);
    }
}
